<?php
namespace app\shop\service\company;
use app\shop\model\company\StudyFile;
use app\common\service\Result;
use app\shop\model\user\User;
class Study
{
    public function lists($data)
    {
        $app_id = request()->app_id;
        $type = $data['type'] ?? 0;
        $map = [
            ['app_id', '=', $app_id],
        ];
        if($type){
            $map[] = ['type', '=', $type];
        }
        $status = $data['status'] ?? -1;
        if ($status != -1) {
            $map[] = ['status', '=', $status];
        }
        $page_size = $data['page_size'] ?? 20;
        $is_app = $data['is_app'] ?? 0;
        $query = StudyFile::where($map);
        if($is_app){
            $query->with(['user']);
        }
        else{
            $query->with(['user', 'admin','staff'=>function($q1){
                $q1->with(['department','station']);
            }]);
        }
        $lists = $query->order('id desc')->append(['status_text','type_text'])->paginate($page_size);
        return Result::success($lists);
    }

    public function my_share($data)
    {
        $app_id = request()->app_id;
        $type = $data['type'] ?? 1;
        $user_id = $data['user_id'] ?? 0;
        $map = [
            ['app_id', '=', $app_id],
            ['type', '=', $type],
        ];
        if ($user_id) {
            $map[] = ['user_id', '=', $user_id];
        }
        $page_size = $data['page_size'] ?? 20;
        $lists = StudyFile::where($map)->with(['user', 'admin'])->append(['status_text'])
            ->order('id desc')->paginate($page_size);
        return Result::success($lists);
    }

    public function add($data)
    {
        $data['app_id'] = request()->app_id;
        unset($data['id']);
        StudyFile::create($data);
        return Result::success();
    }

    public function check($data)
    {
        try{
            $info = StudyFile::find($data['id']);
            if (!$info) {
                return Result::error('数据不存在');
            }
            $status = $data['status'] ?? 1;
            $points = $data['points'] ?? 0;
            $reason = $data['reason'] ?? '';
            if($status==1){
                if($points<=0){
                    return Result::error('请填写奖励积分');
                }
                //增加积分
                $model = User::detail($info->user_id);
                $admin = request()->user['user_name'];
                $model->recharge($admin, 1, [
                    'points'=>[
                        'mode'=>'inc',
                        'remark'=>'学习分享奖励',
                        'value'=>$points,
                    ]
                ]);
            }
            if($status==2 && !$reason){
                return Result::error('请填写驳回原因');
            }
            $data['checker'] = request()->user_id;
            $info->save($data);
            return Result::success();
        }
        catch (\Exception $e) {
            return Result::error($e->getMessage());
        }
    }

    public function delete($id)
    {
        $info = StudyFile::find($id);
        if (!$info) {
            return Result::error('数据不存在');
        }
        $info->delete();
        return Result::success();
    }

    public function info($id)
    {
        $info = StudyFile::with(['user', 'file'])->append(['status_text'])->find($id);
        if ($info->user_id == request()->user_id) {
            $info->is_self = 1;
        } else {
            $info->is_self = 0;
        }
        return Result::success($info);
    }
}